package com.kedou.net;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.kedou.app.Config;
import com.kedou.app.R;
import com.kedou.domain.ProjectItem;
import com.kedou.net.PostLogin.FailCallback;
import com.kedou.net.PostLogin.SuccessCallback;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;

/**
 * 后台请求：获取项目列表
 * 
 * @author zhouchao
 * 
 */
public class PostProjectList {
	public PostProjectList(String token, String userId, int page, String os,
			String deviceId, final SuccessCallback successCallback,
			final FailCallback failCallback, final Context context) {
		new NetConnection(
				Util.getSDBaseUrl() + Config.URL_GETPROJECTLIST,
				HttpMethod.POST,
				new NetConnection.SuccessCallback() {

					@Override
					public void onSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							switch (obj.getInt(Config.KEY_STATUSCODE)) {
							case Config.RESULT_STATUS_SUCCESS:
								if (successCallback != null) {
									if (obj.has("data")) {
										JSONArray jsonArray = obj
												.getJSONArray("data");
										if (jsonArray != null) {
											ArrayList<ProjectItem> mProjects = new ArrayList<ProjectItem>();
											ArrayList<String> urls = new ArrayList<String>();
											for (int i = 0; i < jsonArray
													.length(); i++) {
												ProjectItem item = new ProjectItem();
												JSONObject jsonObject = (JSONObject) jsonArray
														.get(i);
												if (jsonObject.has("projectId")) {
													item.setProjectId(jsonObject
															.getString("projectId"));
												}
												if (jsonObject.has("userLevel")) {
													item.setUser_level(jsonObject
															.getString("userLevel"));
												}
												if (jsonObject
														.has("projectName")) {
													item.setProjectName(jsonObject
															.getString("projectName"));
												}
												if (jsonObject
														.has("projectSlogan")) {
													item.setProjectSlogan(jsonObject
															.getString("projectSlogan"));
												}
												if (jsonObject.has("subTitile")) {
													item.setSubTitle(jsonObject
															.getString("subTitile"));
												}
												if (jsonObject
														.has("createTime")) {
													item.setCreateTime(jsonObject
															.getString("createTime"));
												}
												if (jsonObject.has("endTime")) {
													item.setEndTime(jsonObject
															.getString("endTime"));
												}
												if (jsonObject.has("progress")) {
													item.setProgress(jsonObject
															.getString("progress"));
												}
												if (jsonObject.has("paid")) {
													item.setPaid(jsonObject
															.getString("paid"));
												}
												if (jsonObject.has("goal")) {
													item.setGoal(jsonObject
															.getString("goal"));
												}
												if (jsonObject.has("detail")) {
													JSONObject jsonObject2 = jsonObject
															.getJSONObject("detail");
													if (jsonObject2
															.has("video")) {
														item.setVideoUrl(jsonObject2
																.getString("video"));
													}
													if (jsonObject2
															.has("accounting")) {
														item.setAccounting(jsonObject2
																.getString("accounting"));
													}
													if (jsonObject2
															.has("followNum")) {
														item.setFollowNum(jsonObject2
																.getString("followNum"));
													}
													if (jsonObject2
															.has("projectStatus")) {
														item.setProjectStatus(jsonObject2
																.getInt("projectStatus"));
													}
													if (jsonObject2
															.has("introduction")) {
														item.setIntroduction(jsonObject2
																.getString("introduction"));
													}
													if (jsonObject2
															.has("hasFollowed")) {
														item.setHasFollowed(jsonObject2
																.getInt("hasFollowed"));
													}
													if (jsonObject2
															.has("typeUrl1")) {
														urls.add(jsonObject2
																.getString("typeUrl1"));
													}
													if (jsonObject2
															.has("typeUrl2")) {
														urls.add(jsonObject2
																.getString("typeUrl2"));
													}
													if (jsonObject2
															.has("typeUrl3")) {
														urls.add(jsonObject2
																.getString("typeUrl3"));
													}
													if (jsonObject2
															.has("typeUrl4")) {
														urls.add(jsonObject2
																.getString("typeUrl4"));
													}
													if (jsonObject2
															.has("typeUrl5")) {
														urls.add(jsonObject2
																.getString("typeUrl5"));
													}
													if (jsonObject2
															.has("typeUrl6")) {
														urls.add(jsonObject2
																.getString("typeUrl6"));
													}
													if (jsonObject2
															.has("typeUrl7")) {
														urls.add(jsonObject2
																.getString("typeUrl7"));
													}
													item.setUrls(urls);
												}
												mProjects.add(item);
											}
											successCallback.onSuccess(
													obj.getString(Config.KEY_STATUSMSG),
													mProjects);
										}
									}
								}
								break;
							default:
								if (failCallback != null) {
									failCallback.onFail(obj
											.getString(Config.KEY_STATUSMSG));
								}
								break;
							}

						} catch (JSONException e) {
							e.printStackTrace();
							if (failCallback != null) {
								failCallback.onFail(context.getResources()
										.getString(R.string.server_have_wrong));
							}
						}
					}
				}, new NetConnection.FailCallback() {

					@Override
					public void onFail() {
						if (failCallback != null) {
							failCallback.onFail(context.getResources()
									.getString(R.string.server_have_wrong));
						}
					}
				}, Config.KEY_TOKEN, token, Config.KEY_USERID, userId,
				Config.KEY_PAGE, page + "", Config.KEY_OS, os,
				Config.KEY_DEVICE, deviceId);
	}

	public static interface SuccessCallback {
		void onSuccess(String msg, ArrayList<ProjectItem> projects);
	}

	public static interface FailCallback {
		void onFail(String msg);
	}
}
